<template>
	<view class="content">
		<view class="contentTop">
			<view class="imgName">
				<image src="@/static/logo.png" alt="" />
				<span>宋春磊</span>
			</view>
		</view>
		<view class="contentHead">
			<view class="field">
				<span>e捷通推广码</span>
				<span class="span">99999</span>
			</view>
			<view class="field">
				<span>操作手册</span>
				<span></span>
			</view>
			<view class="field" @click="handleJump">
				<span>设置</span>
				<span></span>
			</view>
			<view class="field">
				<span>版本号</span>
				<span>2.00.0</span>
			</view>
		</view>
		
		<view class="" v-if="hostName.value == 'app'">
			<button type="primary" @click="checkIsSupportSoterAuthentication">检查支持的认证方式</button>
			<button type="primary" @click="checkIsSoterEnrolledInDeviceFingerPrint">检查是否录入指纹</button>
			<button type="primary" @click="checkIsSoterEnrolledInDeviceFaceID">检查是否录入FaceID</button>
			<button type="primary" @click="startSoterAuthenticationFingerPrint">开始指纹认证</button>
			<button type="primary" @click="startSoterAuthenticationFaceID">开始FaceID认证</button>
		</view>

	</view>
</template>

<script setup>
	import {ref,computed,reactive} from 'vue';
	import {onLoad,onShow} from "@dcloudio/uni-app"
	import {useStore} from "vuex"
	const store = useStore();
	const phoneOS = computed(() => store.state.phoneOS);
	const hostName = computed(() => store.state.hostName);
	const errMsgList = reactive([{
			code: 0,
			msg: '识别成功'
		},
		{
			code: 90001,
			msg: '本设备不支持生物认证'
		},
		{
			code: 90002,
			msg: '用户未授权使用该生物认证接口'
		},
		{
			code: 90003,
			msg: '请求使用的生物认证方式不支持'
		},
		{
			code: 90004,
			msg: '未传入challenge或challenge长度过长（最长512字符）'
		},
		{
			code: 90005,
			msg: 'auth_content长度超过限制（最长42个字符）'
		},
		{
			code: 90007,
			msg: '内部错误'
		},
		{
			code: 90008,
			msg: '用户取消授权'
		},
		{
			code: 90009,
			msg: '识别失败'
		},
		{
			code: 90010,
			msg: '重试次数过多被冻结'
		},
		{
			code: 90011,
			msg: '用户未录入所选识别方式'
		}
	])

	onLoad(() => {
		console.log(phoneOS.value, hostName.value);
		// 是app走生物识别，否则不走
		if (hostName.value == 'app') {
			checkIsSupportSoterAuthentication()
			console.log(hostName)
		} else {
			console.log(hostName.value);
		}
	});
	onShow(() => {});
	const handleJump = () =>{
		uni.navigateTo({
			url:'/pages/SetPage/SetPage'
		})
	}

	// 检查支持的录入方式android为fingerPrint,ios为facial
	const checkIsSupportSoterAuthentication = () => {
		uni.checkIsSupportSoterAuthentication({
			success(res) {
				console.log(res);
				let auType = res.supportMode[0]
				if (auType == 'fingerPrint' && phoneOS.value == 'android') {
					uni.showToast({
						title: '指纹识别',
						icon: 'none',
						// duration: 1000
					});
					// 检查是否录入指纹
					setTimeout(() => {
						checkIsSoterEnrolledInDeviceFingerPrint()
					}, 2000)

				} else if (auType == 'facial' && phoneOS.value == 'ios') {
					uni.showToast({
						title: '人脸识别',
						icon: 'none',
						// duration: 1000
					});
					setTimeout(() => {
						checkIsSoterEnrolledInDeviceFaceID()
					}, 2000)
				} else {
					uni.showToast({
						title: '不支持生物识别',
						icon: 'error',
						// duration: 1000
					});
				}
			},
			fail(err) {
				console.log(err);
			},
			complete(res) {
				console.log(res);
			}
		})
	};
	const checkIsSoterEnrolledInDeviceFingerPrint = () => {
		uni.checkIsSoterEnrolledInDevice({
			checkAuthMode: 'fingerPrint',
			success(res) {
				console.log(res);
				// if (res.isEnrolled) {
				// 	uni.showToast({
				// 		title: '指纹有录入',
				// 		icon: 'none',
				// 		duration: 1000
				// 	});
				// }
			},
			fail(err) {
				console.log(err);
			},
			complete(res) {
				console.log(res);
				if (res.isEnrolled) {
					uni.showToast({
						title: '指纹有录入',
						icon: 'success',
					});
					// 进行解锁
					setTimeout(() => {
						startSoterAuthenticationFingerPrint()
					}, 2000)
				} else {
					uni.showToast({
						title: '指纹没有录入',
						icon: 'error',
					});
				}
			}
		})
	};
	const checkIsSoterEnrolledInDeviceFaceID = () => {
		uni.checkIsSoterEnrolledInDevice({
			checkAuthMode: 'facial',
			success(res) {
				console.log(res);
			},
			fail(err) {
				console.log(err);
			},
			complete(res) {
				console.log(res);
				if (res.isEnrolled) {
					uni.showToast({
						title: '人脸有录入',
						icon: 'success',
					});
					setTimeout(() => {
						startSoterAuthenticationFaceID()
					}, 2000)
				} else {
					uni.showToast({
						title: '人脸没有录入',
						icon: 'error',
					});
				}
			}
		})
	};
	const startSoterAuthenticationFingerPrint = () => {
		uni.startSoterAuthentication({
			requestAuthModes: ['fingerPrint'],
			challenge: '123456',
			authContent: '请用指纹解锁',
			success(res) {
				console.log(res);
			},
			fail(err) {
				console.log(err);
			},
			complete(res) {
				console.log(res);
				if (res.errCode == 0) {
					uni.showToast({
						title: '指纹识别成功',
						icon: 'success',
					});
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						});
					}, 2000)
				} else {
					let errMsg = '未知错误';
					for (let i = 0; i < errMsgList.length; i++) {
						if (errMsgList[i].code == res.errCode) {
							errMsg = errMsgList[i].msg;
						}
					}
					uni.showToast({
						title: errMsg,
						icon: 'error',
					});
				}
			}
		})
	};
	const startSoterAuthenticationFaceID = () => {
		uni.startSoterAuthentication({
			requestAuthModes: ['facial'],
			challenge: '123456',
			authContent: '请用FaceID解锁',
			success(res) {
				console.log(res);
			},
			fail(err) {
				console.log(err);
			},
			complete(res) {
				console.log(res);
				if (res.errCode == 0) {
					uni.showToast({
						title: '人脸识别成功',
						icon: 'success',
					});
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						});
					}, 2000)
				} else {
					let errMsg = '未知错误';
					for (let i = 0; i < errMsgList.length; i++) {
						if (errMsgList[i].code == res.errCode) {
							errMsg = errMsgList[i].msg;
						}
					}
					uni.showToast({
						title: errMsg,
						icon: 'error',
					});
				}
			}
		})
	}
	

</script>

<style lang="scss" scoped>	
	.content{
		.contentTop{
			width: 100%;
			height: 400rpx;
			background-color: #EE9845;
			position: relative;
			.imgName{
				position: absolute;
				bottom: 20rpx;
				left: 20rpx;
				image{
					width: 250rpx;
					height: 250rpx;
					border-radius: 50%;
					overflow: hidden;
					vertical-align:middle;
				}
				span{
					font-size: 35rpx;
					font-weight: bold;
				}
			}
		}
		.contentHead{
			.field{
				height: 90rpx;
				line-height: 90rpx;
				padding: 0 30rpx;
				margin: 0 15rpx 0 20rpx;
				border-bottom: 1px solid #eaeaea;
				font-weight: bold;
				display: flex;
				justify-content: space-between;
				span:not(:nth-child(1)){
					color:#BEBEBE;
					font-weight: normal;
				}
				span:nth-child(2)::after{
					content: '>';
					color: #BEBEBE;
					margin-left: 30rpx;
				}
			}
		}
	}
	
</style>
